using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Campus
{
    class Projectile : BaseEntity
    {
        public static List<Texture2D> animationList = new List<Texture2D>();

        protected Vector2 m_destination;
        public Vector2 Destination
        {
            get { return m_destination; }
            set { m_destination = value; }
        }
        protected float moveSpeed = 100;
        public float MoveSpeed
        {
            get { return moveSpeed; }
            set { moveSpeed = value; }
        }
        protected int m_minimumDamage;
        public int MinimumDamage
        {
            get { return m_minimumDamage; }
        }
        protected int m_maximumDamage;
        public int MaximumDamage
        {
            get { return m_maximumDamage; }
        }
        protected BaseEntity m_target;
        public BaseEntity Target
        {
            get { return m_target; }
            set { m_target = value; }
        }
        protected Ability m_owner;
        public Ability Owner {
            get { return m_owner; }
        }
        public Projectile(BaseEntity target, Vector2 position, Animation animation, Ability owner, int minDamage, int maxDamage) 
            : base(animation, position, 100000, owner.Owner.PlayerOwner, owner.Owner.PlayerOwner.CurrentLevel, 
            new Dictionary<string,Ability>(), new Dictionary<string,AbilitySubMenu>())
        {
            owner.Owner.PlayerOwner.Added.Add(this);
            m_owner = owner;
            Target = target;
            m_destination.X = target.Position.X + target.Size.X / 2;
            m_destination.Y = target.Position.Y + target.Size.Y / 2;            

            m_minimumDamage = minDamage;
            m_maximumDamage = maxDamage;

            Size = new Vector2(30, 30);

            double differenceX = m_destination.X - (Position.X + Size.X / 2.0f);
            double differenceY = m_destination.Y - (Position.Y + Size.Y / 2.0f);

            Rotation = (float)Math.Atan2(differenceY, differenceX);

            CurrentAnimation.Start();
        }

        static public Animation CreateAnimation()
        {
            return new Animation(animationList, 200, false);
        }

        public override void Update(GameTime gameTime, bool checkInput, bool updateBoundingBox)
        {
            //m_destination.X = (m_target.Position.X - m_target.Origin.X);
            //m_destination.Y = (m_target.Position.Y - m_target.Origin.Y);

            //Vector2 boundingCircleOffset = new Vector2();
            //boundingCircleOffset.X = Position.X - BoundingSphere.Center.X;
            //boundingCircleOffset.Y = Position.Y - BoundingSphere.Center.Y;

            m_destination.X = Target.BoundingSphere.Center.X;
            m_destination.Y = Target.BoundingSphere.Center.Y;

            BoundingSphere checkSphere = new BoundingSphere(new Vector3(Position.X, Position.Y, 0), 0.0f);

            Move(gameTime);
            //does everything have a bounding sphere?
            if (Target.BoundingSphere.Intersects(checkSphere)) {
                int damage = new Random().Next(m_minimumDamage, m_maximumDamage + 1);
                Target.TakeDamage(damage);
                this.Destroy();
            }

            base.Update(gameTime, checkInput, updateBoundingBox);
        }
        protected override void Destroy()
        {
            Owner.Owner.PlayerOwner.Destroyed.Add(this);
        }

        public void Move(GameTime gameTime)
        {
            Vector2 direction = (-this.Position + m_destination);
            if (direction.Length() < 4)
                return;
            direction.Normalize();
            this.Position += (direction * moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds);
        }

        public override string ToString()
        {
            return "";
        }
        public override Vector2 GetCenter() {
            return Position;
        }
        
    }
}